package com.gtpickupapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.LightingColorFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

public class SportExpand extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.sportexpand);
		 
		 
		 String result = "";
			InputStream is = null;	
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("eventID" ,Integer.toString(getIntent().getExtras().getInt("eventID"))));
			//http post
			try{
					
			        HttpClient httpclient = new DefaultHttpClient();
			        HttpPost httppost = new HttpPost("http://gtplay.webatu.com/viewEvent.php");
			        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			        HttpResponse response = httpclient.execute(httppost);
			        HttpEntity entity = response.getEntity();
			        is = entity.getContent();
			}catch(Exception e){
			        Log.e("log_tag", "Error in http connection "+e.toString());
			}
			
			//convert response to string
			try{
			        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			        StringBuilder sb = new StringBuilder();
			        String line = null;
			        while ((line = reader.readLine()) != null) {
			                sb.append(line + "\n");
			        }
			        is.close();
			 
			        result=sb.toString();
			}catch(Exception e){
			        Log.e("log_tag", "Error converting result "+e.toString());
			}
			 
			//parse json data
			try{
					
			        
					JSONArray jArray = new JSONArray(result);
					/* Find Tablelayout defined in main.xml */
					TableLayout tl = (TableLayout)findViewById(R.id.sporttableexpanded);
					
					if(jArray.length()> 0){
						JSONObject json_data = jArray.getJSONObject(0);
				
				/*------------------------------------------------*/		
						TableRow tr = new TableRow(this);
			            tr.setLayoutParams(new LayoutParams(
			            	LayoutParams.FILL_PARENT,
			            	LayoutParams.WRAP_CONTENT));
			            
			            /*Create a textView for the name of the event */
			            TextView eventName = new TextView(this);
			            eventName.setText(json_data.getString("Name"));
			            eventName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			            	
			            /* Add views to row. */
			            tr.addView(eventName);
			                 
			            /* Add row to TableLayout. */
			            tl.addView(tr,new TableLayout.LayoutParams(
			                 LayoutParams.FILL_PARENT,
			                 LayoutParams.WRAP_CONTENT));
			            
			     /*----------------------------------------------*/		            
			            TableRow tr2 = new TableRow(this);
			            tr2.setLayoutParams(new LayoutParams(
			            	LayoutParams.FILL_PARENT,
			            	LayoutParams.WRAP_CONTENT));
			            
			            /*Create a textView for the name of the event */
			            TextView location = new TextView(this);
			            location.setText(json_data.getString("Location"));
			            location.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			            	
			            /* Add views to row. */
			            tr2.addView(location);
			                 
			            /* Add row to TableLayout. */
			            tl.addView(tr2,new TableLayout.LayoutParams(
			                 LayoutParams.FILL_PARENT,
			                 LayoutParams.WRAP_CONTENT));
			            
			        /*-----------------------------------------------------------*/    
			            TableRow tr3 = new TableRow(this);
			            tr3.setLayoutParams(new LayoutParams(
			            	LayoutParams.FILL_PARENT,
			            	LayoutParams.WRAP_CONTENT));
			            
			            /*Create a textView for the name of the event */
			            TextView startTime = new TextView(this);
			            startTime.setText("Start: " + json_data.getString("Begin_Time"));
			            startTime.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			            	
			            /* Add views to row. */
			            tr3.addView(startTime);
			                 
			            /* Add row to TableLayout. */
			            tl.addView(tr3,new TableLayout.LayoutParams(
			                 LayoutParams.FILL_PARENT,
			                 LayoutParams.WRAP_CONTENT));
			         /*-------------------------------------------------------------*/   
			            TableRow tr4 = new TableRow(this);
			            tr4.setLayoutParams(new LayoutParams(
			            	LayoutParams.FILL_PARENT,
			            	LayoutParams.WRAP_CONTENT));
			            
			            /*Create a textView for the name of the event */
			            TextView endTime = new TextView(this);
			            endTime.setText("End: " + json_data.getString("End_Time"));
			            endTime.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			            	
			            /* Add views to row. */
			            tr4.addView(endTime);
			                 
			            /* Add row to TableLayout. */
			            tl.addView(tr4,new TableLayout.LayoutParams(
			                 LayoutParams.FILL_PARENT,
			                 LayoutParams.WRAP_CONTENT));
			        /*--------------------------------------------------------------*/    
			            TableRow tr5 = new TableRow(this);
			            tr5.setLayoutParams(new LayoutParams(
			            	LayoutParams.FILL_PARENT,
			            	LayoutParams.WRAP_CONTENT));
			            
			            /*Create a textView for the name of the event */
			            TextView date = new TextView(this);
			            date.setText("Date: " + json_data.getString("Date"));
			            date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			            	
			            /* Add views to row. */
			            tr5.addView(date);
			                 
			            /* Add row to TableLayout. */
			            tl.addView(tr5,new TableLayout.LayoutParams(
			                 LayoutParams.FILL_PARENT,
			                 LayoutParams.WRAP_CONTENT));
			            
			         /*---------------------------------------------------------------*/   
			            TableRow tr6 = new TableRow(this);
			            tr6.setLayoutParams(new LayoutParams(
			            	LayoutParams.FILL_PARENT,
			            	LayoutParams.WRAP_CONTENT));
			            
			            /*Create a textView for the name of the event */
			            TextView type = new TextView(this);
			            type.setText("Type: " + json_data.getString("Type"));
			            type.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			            	
			            /* Add views to row. */
			            tr6.addView(type);
			                 
			            /* Add row to TableLayout. */
			            tl.addView(tr6,new TableLayout.LayoutParams(
			                 LayoutParams.FILL_PARENT,
			                 LayoutParams.WRAP_CONTENT));
			          /*---------------------------------------------------------------*/  
			            TableRow tr7 = new TableRow(this);
			            tr7.setLayoutParams(new LayoutParams(
			            	LayoutParams.FILL_PARENT,
			            	LayoutParams.WRAP_CONTENT));
			            
			            /*Create a textView for the name of the event */
			            TextView attend = new TextView(this);
			            attend.setText("Attendees!");
			            attend.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			            	
			            /* Add views to row. */
			            tr7.addView(attend);
			                 
			            /* Add row to TableLayout. */
			            tl.addView(tr7,new TableLayout.LayoutParams(
			                 LayoutParams.FILL_PARENT,
			                 LayoutParams.WRAP_CONTENT));
			            
			        }
					
			        
			        for(int i=0;i<jArray.length();i++){
			        	JSONObject json_data = jArray.getJSONObject(i);
			        	TableRow tr = new TableRow(this);
			            tr.setLayoutParams(new LayoutParams(
			                           LayoutParams.FILL_PARENT,
			                           LayoutParams.WRAP_CONTENT));
			            
			            	  /*Create a textView for the name of the event */
			            	TextView text = new TextView(this);
			            	text.setText(json_data.getString("User_Name"));
			            	LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			            	lp.setMargins(18, 0, 0, 0);
			            	
			            	
			            	text.setLayoutParams(lp);
			            	
			            	
			                 /* Add Button to row. */
			                 tr.addView(text);
			                 
			       /* Add row to TableLayout. */
			       tl.addView(tr,new TableLayout.LayoutParams(
			                 LayoutParams.FILL_PARENT,
			                 LayoutParams.WRAP_CONTENT));
			   	
			        }
			        
			        
			        
			        //ADD the row for the join button!
			        TableRow tr8 = new TableRow(this);
		            tr8.setLayoutParams(new LayoutParams(
		            	LayoutParams.FILL_PARENT,
		            	LayoutParams.WRAP_CONTENT));
		            
		            /*Create a button */
		            Button b = new Button(this);
	                b.setText("Join");
	                b.setLayoutParams(new LayoutParams(
	                		  LayoutParams.WRAP_CONTENT,
	                            LayoutParams.WRAP_CONTENT));
	                b.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xC6930A));
	      	        b.setOnClickListener(new View.OnClickListener() {
	      	        	public void onClick(View v) {
	      	        		Bundle bundle = getIntent().getExtras();
	      	        		try{
	      	        				String result = "";
	      		        			InputStream is = null;
	      		        			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	      		        			nameValuePairs.add(new BasicNameValuePair("userName",getIntent().getExtras().getString("uName")));
	      		        			nameValuePairs.add(new BasicNameValuePair("eventID",Integer.toString(getIntent().getExtras().getInt("eventID"))));
	      		        			
			        			//TODO add username to the bundle and redirect them to the home screen!
			        		        HttpClient httpclient = new DefaultHttpClient();
			        		        HttpPost httppost = new HttpPost("http://gtplay.webatu.com/addUserEvent.php");
			        		        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			        		        HttpResponse response = httpclient.execute(httppost);
			        		        HttpEntity entity = response.getEntity();
			        		        is = entity.getContent();
			        		        
			        		        //code to redirect on successful signup
			        		        Bundle bundle2 = new Bundle();
			                		bundle2.putString("uName", getIntent().getExtras().getString("uName"));	
			                		//bundle.putString("name",fName + " " + lName );
			                		Intent myintent = new Intent(SportExpand.this,MainPage.class);
			                		myintent.putExtras(bundle2);
			                		SportExpand.this.startActivity(myintent);
			                		finish();
			                		//end of redirect code
			                		
			        		}catch(Exception e){
			        		        Log.e("log_tag", "Error in http connection "+e.toString());
			        		}
	      	        		
	      	        		
	      	        	}         
	      	        });	
		            /* Add views to row. */
		            tr8.addView(b);
		                 
		            /* Add row to TableLayout. */
		            tl.addView(tr8,new TableLayout.LayoutParams(
		                 LayoutParams.FILL_PARENT,
		                 LayoutParams.WRAP_CONTENT));
			        
			        
			        
			        
			}
			catch(JSONException e){
			        Log.e("log_tag", "Error parsing data "+e.toString());
			}
		 
	}
	
	
}
